import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:xiongmao_clean_flutter_module/business/common/workpost/iview/work_rules_save_iview.dart';
import 'package:xiongmao_clean_flutter_module/mvp/base_page_presenter.dart';
import 'package:xiongmao_clean_flutter_module/net/http_config.dart';

import '../../../../net/dio_utils.dart';
import '../../../../net/http_api.dart';
import '../../../../util/log_utils.dart';
import '../bean/work_post_manager_entity.dart';
import '../bean/work_rules_data_entity.dart';
import '../controller/work_post_manager_page_controller.dart';
import '../controller/work_rules_controller.dart';
import '../iview/work_post_iview.dart';

/**
 * 用工规则
 */
class WorkRulesPagePresenter extends BasePagePresenter<WorkRulesSaveIView> with WidgetsBindingObserver {
  WorkRulesPageController controller;

  WorkRulesPagePresenter(this.controller);

  //获取用户规则
  Future<dynamic> getWorkRulesInfo() {
    var params = <String, String>{};
    return requestNetwork<WorkRulesDataEntity>(Method.get, url: HttpApi.WORK_RULES_INFO, queryParameters: params, isShow: true, isClose: true, onSuccess: (data) {
      if (data != null) {
        controller.updateMaxAge(data.maxAge);
        controller.updateSalaryDate(data.payrollDate);
        controller.updateEntryBeforeToday(data.isEntryBeforeToday == "1" ? true : false);
        controller.updateUploadNotGuilty(data.isUploadNotGuilty == "1" ? true : false);
        controller.updateEntrySign(data.isEntrySign == "1" ? true : false);
      }
    }, onError: (_, __) {});
  }

  //编辑状态
  Future<dynamic> updateWorkRules(String? max_age, String? is_entry_before_today, String? is_upload_not_guilty, String? is_entry_sign, String? payroll_date) {
    var params = <String, String>{};
    if (!TextUtil.isEmpty(max_age)) {
      params["max_age"] = "$max_age";
    }
    if (!TextUtil.isEmpty(is_entry_before_today)) {
      params["is_entry_before_today"] = "$is_entry_before_today";
    }
    if (!TextUtil.isEmpty(is_upload_not_guilty)) {
      params["is_upload_not_guilty"] = "$is_upload_not_guilty";
    }
    if (!TextUtil.isEmpty(is_entry_sign)) {
      params["is_entry_sign"] = "$is_entry_sign";
    }
    if (!TextUtil.isEmpty(payroll_date)) {
      params["payroll_date"] = "$payroll_date";
    }
    return requestNetwork<Object>(Method.post, url: HttpApi.SAVE_WORK_RULES_INFO, queryParameters: params, isShow: true, isClose: true, onSuccess: (data) {
      view.workRulesSaveStatus();
    }, onError: (_, __) {});
  }
}
